#!/usr/local/bin/pythonw
# -*- coding: utf-8 -*-

import os, sys, shutil
#import pprint
from dataStructures import *
#import Image
#import numpy
from dimeco_optparser import opt_parser_dimeco_index
#from BeautifulSoup import BeautifulSoup

def main(args=sys.argv[1:]) :
  parser = opt_parser_dimeco_index()

  (opt_options, opt_args) = parser.parse_args(args)
  opt_fileout = opt_options.fileout

  dir_corpus = opt_options.directory
  dir_tgt = opt_options.target_directory

  corpus_index = Corpus_index(opt_options.directory,opt_options.index)
  index = corpus_index.index

  for domain,list_i in index.iteritems() :
    dir_domain = os.path.join(dir_tgt,domain)
    if(os.path.isdir(dir_domain) == False) :
      os.makedirs(dir_domain)

    for i in list_i :
      filename = 'fichier%i.html'%(i)
      filepath = os.path.join(dir_corpus,filename)      
      newfilepath = os.path.join(dir_domain,filename)
      try :
        shutil.copyfile(filepath,newfilepath)
      except :
        print "error : %s"%(filepath)

if __name__ == "__main__":
    main()
